package com.google.code.mochaccino.framework.crypto;

/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.security.Provider;
import java.security.Provider.Service;
import java.security.Security;
import java.util.Set;
import java.util.TreeSet;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

public final class EncryptionProvider {

	private EncryptionProvider() {
	}

	public static String getBouncyCastleName() {
		return getBouncyCastleProvider().getName();
	}

	public static Set<String> getServices() {
		Set<String> services = new TreeSet<String>();
		for ( Provider provider : Security.getProviders() ) {
			services.addAll( getServices( provider ) );
		}
		return services;
	}

	public static Set<String> getServices( Provider provider ) {
		Set<String> services = new TreeSet<String>();
		for ( Service service : provider.getServices() ) {
			services.add( String.format( "Type: \t\t\t\t\t%s\nAlgorithmn: \t\t\t%s\nProvider: \t\t\t\t%s\nString Representation:\t%s", service.getType(), service.getAlgorithm(), provider.getName(), service.toString() ) );
		}
		return services;
	}

	public static void main( String[] args ) {
		// printServices();
		printServices( getBouncyCastleProvider() );
	}

	public static BouncyCastleProvider getBouncyCastleProvider() {
		if ( Security.getProvider( BouncyCastleProvider.PROVIDER_NAME ) == null ) {
			Security.addProvider( new BouncyCastleProvider() );
		}
		return (BouncyCastleProvider) Security.getProvider( BouncyCastleProvider.PROVIDER_NAME );
	}

	public static void printServices( Provider provider ) {
		Set<String> services = getServices( provider );
		for ( String s : services ) {
			System.out.println( s );
		}
	}

	public static void printServices() {
		Set<String> services = getServices();
		for ( String s : services ) {
			System.out.println( s );
		}
	}
}
